package com.atguigu.tingshu.search.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
public class ThreadPoolExecutorConfig {
    //自定义线程池
    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        //  自定义线程池：因为工具类创建的要么阻塞队列要么最大线程数21亿 (INTEGER.MAX_VALUE_)
        //  获取当前cpu核数
        int cpuCoreNum = Runtime.getRuntime().availableProcessors();
        System.out.println("cpuCoreNum = " + cpuCoreNum);
        return new ThreadPoolExecutor(
                cpuCoreNum,//  核心线程数
                cpuCoreNum * 2,//  最大线程数
                3,//  空闲线程等待时间
                TimeUnit.SECONDS,//  时间单位
                new ArrayBlockingQueue<>(100),//  阻塞队列
                Executors.defaultThreadFactory(),//  线程工厂
                new ThreadPoolExecutor.AbortPolicy()//  拒绝策略
        );
    }
}
